package com.suxin.plugin.operator;

import com.suxin.plugin.intergration.IntegrationConfiguration;
import com.suxin.plugin.intergration.factory.DefaultPluginFactory;
import com.suxin.plugin.intergration.factory.PluginFactory;
import com.suxin.plugin.intergration.manager.PluginWrapper;
import com.suxin.plugin.intergration.manager.plugin.PluginManager;
import com.suxin.plugin.intergration.manager.user.PluginUser;
import com.suxin.plugin.listener.PluginListenerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;

import java.util.List;

/**
 * @author Tang
 * @classname DefaultPluginOperator
 * @description [  ]
 * @date 2022/7/4 23:08
 */
@Slf4j
public class DefaultPluginOperator implements PluginOperator{

    private boolean isInit;

    private IntegrationConfiguration integrationConfiguration;
    private ApplicationContext applicationContext;
    private PluginManager pluginManager;
    private PluginUser pluginUser;
    private PluginFactory pluginFactory;

    public DefaultPluginOperator(IntegrationConfiguration integrationConfiguration, ApplicationContext applicationContext,
                                 PluginManager pluginManager, PluginUser pluginUser, PluginListenerFactory listenerFactory) {
        this.integrationConfiguration = integrationConfiguration;
        this.pluginManager = pluginManager;
        this.pluginUser = pluginUser;
        this.applicationContext = applicationContext;
        // 插件工厂
        this.pluginFactory = new DefaultPluginFactory(pluginManager,listenerFactory,applicationContext);
    }

    @Override
    public boolean initPlugins() {
        if (isInit) {
            throw new RuntimeException("已经初始化过了！");
        }
        log.info("Plugins start initialize of root path '{}'", pluginManager.getPluginRoot().toString());
        try {
            // 初始化插件
            pluginFactory.initialize();
            // 加载插件
            pluginManager.loadPlugins();
            // 启动插件
            pluginManager.startPlugins();
            List<PluginWrapper> startedPlugins = pluginManager.getStartedPlugins();
            for (PluginWrapper pluginWrapper : startedPlugins) {
                pluginFactory.registry(pluginWrapper);
            }
            pluginFactory.build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("Plugins end initialize of root path '{}'", pluginManager.getPluginRoot().toString());
        isInit = true;
        return isInit;
    }

    @Override
    public boolean start(String pluginId) throws Exception {
        return false;
    }

    @Override
    public List<PluginInfo> getPluginInfo() {
        return null;
    }

    @Override
    public PluginInfo getPluginInfo(String pluginId) {
        return null;
    }

    @Override
    public List<PluginWrapper> getPluginWrapper() {
        return null;
    }

    @Override
    public PluginWrapper getPluginWrapper(String pluginId) {
        return null;
    }
}